/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webservice;

import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParkingspacesModel;
import com.carpark.ejb.Model.ReceivablesModel;
import com.carpark.ejb.Model.TarrifsModel;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.persistence.Temporal;
import javax.validation.constraints.Size;

/**
 *
 * @author Krzysztof
 */
@ManagedBean
@ViewScoped
public class ParkingPassesBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private final DAO dao = getSingleDao();
    DaoSingleObject dso;
    private Integer parkingPassId;
    private Date dateIssue;
    private Date expirationDate;
    private String dateIssueForUSer;
    private String expirationDateForUser;
    @Size(min = 1, max = 10)
    private String passType;
    private TarrifsModel tarrif;
    private CarsModel updatedCar;
    private ParkingspacesModel numberId;
    private ParkingpassesModel selectedPass;
    private List<ParkingpassesModel> passesByClient;
    private SelectItem[] carsByClientForChange;
    private Integer chosenAcc;
    private Integer chosenCar;
    private LoginBean lg;
    private ClientsModel currentUser;
    private String radioOptions;
    private String plate;
    private String make;
    private String model;
    private ReceivablesModel rcv;
    SimpleDateFormat formatter;
    private String carTypeForUser;
    private String carType;
      private CarsModel chosenCarM;
      
    @PostConstruct
    public void init() {
        serverLog("NOWY WIDOK PRZEPUSTKI");
        setCurrentUser(dao.getClientById(getUserID()));
        formatter = new SimpleDateFormat("EEE d MMM yy", new Locale.Builder().setLanguage("pl").setRegion("PL").build());
    }

    public SelectItem[] getCarsByClientForChange() {
        if (currentUser == null) {
            serverLog("ckl");
        }
        if (selectedPass == null) {
            serverLog("sell");
        }
        List<CarsModel> cars = dao.getCarsByClientAndType(currentUser, selectedPass.getNumberId().getType());
        SelectItem[] options = new SelectItem[cars.size() + 1];

        options[0] = new SelectItem("", "Wybierz");
        int i = 1;
        for (CarsModel cr : cars) {
            options[i] = new SelectItem(cr.getCarId(), cr.getMake() + " " + cr.getModel() + " " + cr.getNumberPlate());
            i++;
        }
        return options;

    }

    public ReceivablesModel getRcv() {
        return rcv;
    }

    public void setRcv(ReceivablesModel rcv) {
        this.rcv = rcv;
    }

    public CarsModel getUpdatedCar() {
        return updatedCar;
    }

    public void setUpdatedCar(CarsModel updatedCar) {
        this.updatedCar = updatedCar;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
        this.carTypeForUser=carTypes.Motorcycle.fromValueToLabel(carType);
    }

       public String getCarTypeForUser() {
        
        if(this.chosenCar!=null){
        return carTypes.Motorcycle.fromValueToLabel(dao.getCarById(this.chosenCar).getCarType());
    }
        else{
            return "brak";
        }}

    public void setCarTypeForUser(String carTypeForUser) {
        this.carTypeForUser = carTypeForUser;
    }
      public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.dao;
    }

    public void setCarsByClientForChange(SelectItem[] carsByClientForChange) {
        this.carsByClientForChange = carsByClientForChange;
    }

    public Integer getUserID() {
        if (this.lg == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.lg = (LoginBean) app.evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        }
        return this.lg.getId();
    }

    public CarsModel getChosenCarM() {
        if(chosenCar!=null){
            
        
        return chosenCarM;
    }
        else{
        return null;
        }
    }
        

    public void setChosenCarM(CarsModel chosenCarM) {
        this.chosenCarM = chosenCarM;
    }

    public Integer getParkingPassId() {
        return parkingPassId;
    }

    public LoginBean getLg() {
        return lg;
    }

    public void setLg(LoginBean lg) {
        this.lg = lg;
    }

    public ClientsModel getCurrentUser() {
        return currentUser;
    }

    public String getDateIssueForUSer() {
        return dateIssueForUSer;
    }
      public String getDateIssueForUSer(ParkingpassesModel pass) {
        return  formatter.format(pass.getDateIssue());
    }

    public void setDateIssueForUSer(String dateIssueForUSer) {
        this.dateIssueForUSer = dateIssueForUSer;
    }

    public String getExpirationDateForUser() {
        return expirationDateForUser;
    }

      public String getExpirationDateForUser(ParkingpassesModel pass) {
        return  formatter.format(pass.getExpirationDate());
    }
    public void setExpirationDateForUser(String expirationDateForUser) {
        this.expirationDateForUser = expirationDateForUser;
    }
public String getDateIS(ParkingpassesModel p) {
 this.dateIssueForUSer=     formatter.format(p.getDateIssue());
        return  this.dateIssueForUSer ;
    }
public String getDateExp(ParkingpassesModel p) {
 this.expirationDateForUser=     formatter.format(p.getExpirationDate());
        return   this.expirationDateForUser ;
    }
    public Integer getChosenCar() {
        serverLog("ustawiam id samochodu na : " + chosenCar);
        return chosenCar;
    }

    public String getPlate() {
        return plate;
    }

    public void setPlate(String plate) {
        serverLog("rejestracja: " + plate);
        this.plate = plate;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        serverLog("marka: " + make);
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        serverLog("model: " + model);
        this.model = model;
    }

    public void setChosenCar(Integer chosenCar) {
        serverLog("samochód: " + chosenCar);
        this.chosenCar = chosenCar;
        this.chosenCarM=dao.getCarById(chosenCar);
    }

    public void setCurrentUser(ClientsModel currentUser) {
        this.currentUser = currentUser;
    }

    public void setParkingPassId(Integer parkingPassId) {
        this.parkingPassId = parkingPassId;
    }

    public Date getDateIssue() {
        return dateIssue;
    }

    public Integer getChosenAcc() {
        return chosenAcc;
    }

    public String getRadioOptions() {
        return radioOptions;
    }

    public void setRadioOptions(String radioOptions) {
        serverLog("ustawiam radio " + radioOptions);
        this.radioOptions = radioOptions;
    }

    public void setChosenAcc(Integer chosenAcc) {
        serverLog("wybiera acc: " + chosenAcc);
        this.chosenAcc = chosenAcc;
        getPassesByClient();
    }

    public void setDateIssue(Date dateIssue) {
        this.dateIssue = dateIssue;
     
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
        this.expirationDateForUser=formatter.format(expirationDate);
    }

    public String getPassType() {
        return passType;
    }

    public void setPassType(String passType) {
        this.passType = passType;
    }

    public TarrifsModel getTarrif() {
        return tarrif;
    }

    public void setTarrif(TarrifsModel tarrif) {
        this.tarrif = tarrif;
    }

    public CarsModel getCarId() {
        return updatedCar;
    }

    public void setCarId(CarsModel carId) {
        this.updatedCar = carId;
    }

    public ParkingspacesModel getNumberId() {
        return numberId;
    }

    public void setNumberId(ParkingspacesModel numberId) {
        this.numberId = numberId;
    }

    public ParkingpassesModel getSelectedPass() {
        return selectedPass;
    }

    public void setSelectedPass(ParkingpassesModel selectedPass) {
        this.selectedPass = selectedPass;
           this.dateIssueForUSer=formatter.format(selectedPass.getDateIssue());
           this.expirationDateForUser=formatter.format(selectedPass.getExpirationDate());
    }

    public void addCar() {
        serverLog("ustawia wartosci dla car");
        if (radioOptions.equals("1")) {
            this.updatedCar = dao.getCarById(chosenCar);

        }
        if (radioOptions.equals("2")) {
            serverLog("zapisuje nowy samochód do bazy");
            serverLog(make);
            serverLog(model);
            serverLog(plate);
            this.updatedCar = dao.addCar(new CarsModel(plate, make, model, selectedPass.getNumberId().getType()));
        }

    }
  public void addCarMobile() {
        serverLog("ustawia wartosci dla car");
        if (radioOptions.equals("1")) {
            this.updatedCar = dao.getCarById(chosenCar);

        }
        if (radioOptions.equals("2")) {
            serverLog("zapisuje nowy samochód do bazy");
            serverLog(make);
            serverLog(model);
            serverLog(plate);
            this.updatedCar = new CarsModel(plate, make, model, selectedPass.getNumberId().getType());
        }

    }

    public List<ParkingpassesModel> getPassesByClient() {

        if (chosenAcc == null) {
            setPassesByClient(dao.getPassesByClient(currentUser));
        } else {
            setPassesByClient(dao.getPassesByAccounting(dao.getAccountingDataById(chosenAcc)));
        }

        return this.passesByClient;
    }

    public void serverLog(String x) {
        Logger.getLogger(ParkingPassesBean.class.getName()).log(Level.SEVERE, "[Carpark2013]: " + x);
    }

    public void setPassesByClient(List<ParkingpassesModel> passesByClient) {
        serverLog("nowa lista");
        this.passesByClient = passesByClient;
    }

    public void updateCarinParkingPasses(ActionEvent actionEvent) {

        FacesContext context = FacesContext.getCurrentInstance();
        serverLog("aktualizuje pojazd");
        addCar();
        serverLog("aktualizuje przepustkę " + selectedPass.getParkingPassId());
        ParkingpassesModel ps = dao.getParkingPassById(selectedPass.getParkingPassId());
        ps.setCarId(updatedCar);
        if (dao.updatePass(ps) == null) {
            context.addMessage(null, new FacesMessage("Wystąpił błąd", "Nie udało się zmienić pojazdu"));
        } else {
            setPassesByClient(null);
       refreshPage();
            context.addMessage(null, new FacesMessage("Operacja zakończona sukcesem", "pojazd zmieniono."));
        }

    }

       protected void refreshPage() {
FacesContext fc = FacesContext.getCurrentInstance();
String refreshpage = fc.getViewRoot().getViewId();
ViewHandler ViewH =fc.getApplication().getViewHandler();
UIViewRoot UIV = ViewH.createView(fc,refreshpage);
UIV.setViewId(refreshpage);
fc.setViewRoot(UIV);
} 
    
}
